package com.yonyou.crm.sys.menu.appctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.common.security.util.Md5CodeUtil;
import com.yonyou.crm.sys.appmenu.entity.AppMenuVO;
import com.yonyou.crm.sys.appmenu.rmitf.IAppMenuRmService;

import net.sf.json.JSONObject;


@RequestMapping("/{version}/msys/menus")
@RestController
//@Scope("prototype")
public class AppMenusCtrl {
	
	@Autowired
	IAppMenuRmService service;
	
	@RequestMapping(method = RequestMethod.GET)
	public Object functionList(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		Map<String, Object> data = new HashMap<String, Object>();
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String appversion = paramObj.getString("version");
		Map<String, Object> paraMap = new HashMap<>();
		
		List<AppMenuVO> list = service.getList(paraMap);
		
		Map<String, Object> menuList = new HashMap<>();
		menuList.put("menulist", list);
		
		String hashCodeVersion = Md5CodeUtil.createMd5CodeForMap(menuList);

		if (StringUtils.isBlank(appversion) || (!appversion.equals(hashCodeVersion))) {
			data.put("functionlist", list);
		}
		data.put("version", hashCodeVersion);
		return ResponseUtil.getObject(data);
	}
	
}